package com.example.walkingtour.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WTDatabase extends SQLiteOpenHelper {
	public static final String TAG = "WTDatabase";

	private static final String DATABASE_NAME = "WalkingTour";
	private static final int DATABASE_VERSION = 2;

	public static final String TABLE_RECORDINGS = "Recordings";
	public static final String KEY_ROWID = "_id";
	public static final String COL_FILE = "file";
	public static final String COL_GPS_LAT = "gps_lat";
	public static final String COL_GPS_LONG = "gps_long";
	public static final String COL_PATH = "path";
	public static final String COL_RECORDING_TYPE = "type";
	public static final String COL_TIME = "time";

	private static final String DATABASE_CREATE = "create table "
			+ TABLE_RECORDINGS + " (" + KEY_ROWID
			+ " integer primary key autoincrement, " + COL_FILE
			+ " text not null, " + COL_GPS_LAT + " real not null, "
			+ COL_GPS_LONG + " real not null, " + COL_PATH + " text not null, "
			+ COL_RECORDING_TYPE + " text not null, " + COL_TIME
			+ " integer not null)";

	public WTDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL(DATABASE_CREATE);
			Log.d(TAG, "DB CREATED: " + DATABASE_CREATE);
		} catch (Exception ex) {
			Log.w(TAG, ex.getMessage().toString());
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TAG, "Upgrading database. Existing contents will be lost. ["
				+ oldVersion + "]->[" + newVersion + "]");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDINGS);
		onCreate(db);
	}

	public void close() {
		this.close();
	}

}
